<?php

namespace app\payment;

use app\services\ConfigService;
use Webman\Config;
use Yansongda\Pay\Pay;

class AliPay extends AbstractPay
{
    public $supportPayWay = [
        'web',
        'h5',
        'app',
        'mini',
        'scan'
    ];

    public function pay()
    {
        Pay::config(ConfigService::getPaymentConfig(PayFactory::ALIPAY, $this->data['pay_way_type']));

        $data = [
            'out_trade_no' => $this->data['order_no'],
            'subject' => '支付商品',
            'total_amount' => $this->data['total_amount']
        ];

        switch ($this->data['pay_way_type']) {
            case 'scan':
                $data['_method'] = 'get';
                $res = Pay::alipay()->web($data);
                return ['url' => $res->getHeader('Location')[0] ?? ''];
                break;
            default:
            case 'mp':
            case 'h5':
                $data['_method'] = 'get';
                return Pay::alipay()->wap($data);
                break;
            case 'app':
                return Pay::alipay()->app($data);
        }

    }

    public function payBack()
    {
        Pay::config(ConfigService::getPaymentConfig(PayFactory::ALIPAY));

        return Pay::wechat()->refund([
            'out_trade_no' => $this->data['order_no'],
            'refund_amount' => $this->data['total_amount'] * 100,
            '_action' => $this->data['pay_way_type'],
        ]);
    }
}